package fun.sboy.panel;

import fun.sboy.api.API;
import fun.sboy.listener.MouseCLickListener;
import fun.sboy.pojo.ManagerUser;
import fun.sboy.util.JDBCUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.util.Enumeration;
import java.util.regex.Pattern;

public class RegistryPanel extends BaseJPanel {

    //注册控件
    private JLabel name = new JLabel("姓名    ：");
    private JLabel sex = new JLabel("性别    ：");
    private JLabel username = new JLabel("用户名  ：");
    private JLabel password = new JLabel("密码    ：");
    private JLabel repeatPassword = new JLabel("重复密码：");
    private JLabel phone = new JLabel("电话号码：");
    private JLabel address = new JLabel("家庭住址：");
    private JLabel[] labels = {name, sex, username, password, repeatPassword, phone, address};


    //输入框控件
    private JTextField nameText = new JTextField(30);
    private ButtonGroup sexGroup = new ButtonGroup();
    private JTextField usernameText = new JTextField(30);
    private JTextField passwordText = new JTextField(30);
    private JTextField repeatPasswordText = new JTextField(30);
    private JTextField phoneText = new JTextField(30);
    private JTextField addressText = new JTextField(30);
    private JButton registerBtn = new JButton("注册");
    private JTextField[] textFields = {nameText, null, usernameText, passwordText, repeatPasswordText, phoneText, addressText};


    //共享部分
    public JLabel toLoginPanel = new JLabel("已有账号？立即登录");

    public RegistryPanel(LayoutManager layout, boolean isDoubleBuffered) {
        super(layout, isDoubleBuffered);
        initialization();
    }

    public RegistryPanel(LayoutManager layout) {
        super(layout);
        initialization();
    }

    public RegistryPanel(boolean isDoubleBuffered) {
        super(isDoubleBuffered);
        initialization();
    }

    public RegistryPanel() {
        initialization();
    }

    @Override
    public void initialization() {
        int x = 50, y = 15;
        for (JLabel l : labels) {
            l.setBounds(x, y, 100, 20);
            add(l);
            y += 35;
        }


        //添加输入框
        x = 130;
        y = 10;
        for (int i = 0; i < textFields.length; i++) {
            JTextField textField = textFields[i];
            if (i != 1) {
                textField.setBounds(x, y, 240, 30);
                add(textField);
            }
            y += 35;
        }
        //添加输入框监听事件
        addPhoneTextFieldKeyListener();

        //创建性别相关的控件
        JRadioButton boy = new JRadioButton("男", true);
        JRadioButton girl = new JRadioButton("女");
        boy.setBounds(130, 50, 50, 20);
        girl.setBounds(180, 50, 50, 20);
        sexGroup.add(boy);
        sexGroup.add(girl);
        add(boy);
        add(girl);


        //注册按钮
        registerBtn.setBounds(x + 60, y + 5, 100, 25);
        registerBtn.setContentAreaFilled(false);
        registerBtn.setDefaultCapable(false);
        addRegisterClickListener();
        add(registerBtn);


        //跳转登录按钮
        toLoginPanel.setForeground(new Color(52, 152, 219));
        toLoginPanel.setBounds(310, 290, 150, 20);
        add(toLoginPanel);

    }


    /**
     * 添加手机号码的监听事件
     */
    private void addPhoneTextFieldKeyListener() {
        phoneText.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (c < '0' || c > '9') {
                    if (c != '\n') {
                        e.setKeyChar('\0');
                    }
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {

            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });
    }

    /**
     * 添加点击注册的事件
     */
    private void addRegisterClickListener() {

        registerBtn.addMouseListener(new MouseCLickListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

                String[] values = new String[6];
                for (int i = 0; i < textFields.length; i++) {
                    JTextField t = textFields[i];
                    if (i == 4) continue;
                    if (t != null) {
                        int k = i > 4 ? i - 1 : i;
                        String val = t.getText();
                        values[k] = val;
                    }
                }
                Enumeration<AbstractButton> sexs = sexGroup.getElements();
                while (sexs.hasMoreElements()) {
                    JRadioButton radioButton = (JRadioButton) sexs.nextElement();
                    if (radioButton.isSelected()) {
                        values[1] = radioButton.getText().equals("男") ? "1" : "0";
                    }
                }

                for (int i = 0; i < values.length; i++) {
                    String val = values[i];
                    if (val.equals("")) {
                        int k = i > 3 ? i + 1 : i;
                        JOptionPane.showMessageDialog(null, labels[k].getText() + "\b 输入框不能为空，请填写后再进行注册！！！");
                        return;
                    }
                }

                if (usernameText.getText().length() < 6) {
                    JOptionPane.showMessageDialog(null, "用户名不得小于6位！！！");
                    return;
                }
                if (passwordText.getText().length() < 6) {
                    JOptionPane.showMessageDialog(null, "密码不得小于6位！！！");
                    return;
                }

                if (!passwordText.getText().equals(repeatPasswordText.getText())) {
                    JOptionPane.showMessageDialog(null, "密码不一致，请确认保持一致后提交注册！！！");
                    return;
                }

                String regex = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
                if (!Pattern.matches(regex, phoneText.getText())) {
                    JOptionPane.showMessageDialog(null, "手机号码不正确，请重新填写！！！");
                    return;
                }

                /**
                 * 判断用户名是否存在与数据库
                 */
                if (API.usernameExists(usernameText.getText())) {
                    JOptionPane.showMessageDialog(null, "用户名已经存在，请更换用户名！！！", "用户名已经存在", JOptionPane.ERROR_MESSAGE);
                    return;
                }


                Integer column = API.register(values);
                if (column == 1) {
                    JOptionPane.showMessageDialog(null, "注册成功！！！", "成功", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "注册失败，请检查！！！", "注册失败", JOptionPane.ERROR_MESSAGE);
                }
            }
        });


    }

    @Override
    public void reload() {

    }
}
